home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7884 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  112 lines

  1. Newsgroups: comp.lang.c++,comp.lang.fortran
  2. Path: sldb6.slac.stanford.edu!fairfield
  3. From: fairfield@sldb6.slac.stanford.edu
  4. Subject: Re: Calling C++ from FORTRAN on VMS
  5. Message-ID: <1996Feb15.195246.1@sldb6.slac.stanford.edu>
  6. Sender: news@unixhub.SLAC.Stanford.EDU
  7. Organization: Stanford Linear Accelerator Center
  8. References: <31239D17.3159@stsci.edu>
  9. Date: Fri, 16 Feb 1996 03:52:46 GMT
  10.  
  11. In article <31239D17.3159@stsci.edu>,
  12.                         Scott Stallcup <stallcup@stsci.edu> writes:
  13. > I need to call a C++ routine from a FORTRAN routine on both
  14. > VAX/VMS and AXP/VMS platforms.
  15.  
  16.         This probably should have  been  posted  to comp.os.vms (or even
  17.     comp.sys.dec)  since  there is little specific to either Fortran  or
  18.     C++ involved (except for name mangling, see below).
  19.  
  20. > The C++ documentation fails to mention mixed langage programs
  21. > (other than calling C from C++)...
  22. >
  23. > Given the following example code, what compiler/linker options
  24. > will resolve the c++ reference ?
  25. >
  26. > ---------------------------------------------------------------
  27. >       program tfor
  28. > c
  29. >       call tcxx ()
  30. > c
  31. >       end
  32. > ---------------------------------------------------------------
  33. >
  34. > #include <stdio.h>
  35. >
  36. > void tcxx (void)
  37. >  {
  38. >   printf ("Hello World\n");
  39. >  }
  40. >
  41. > ---------------------------------------------------------------
  42. > $ fortran tfor
  43. > $ cxx tcxx
  44. > $ link tfor,tcxx
  45. > %LINK-W-NUDFSYMS, 1 undefined symbol:
  46. > %LINK-I-UDFSYM,         TCXX
  47. > %LINK-W-USEUNDEF, undefined symbol TCXX referenced
  48. >         in psect $LINK$ offset %X00000020
  49. >         in module TFOR
  50.  
  51.         The problem is  that  your  C++  procedure,  tcxx, has undergone
  52.     C++'s  "name  mangling".   It's external name is not "TCXX",  it  is
  53.     "TCXX__XV".  How did I detemine that?  I used a "dumb" way, and I'll
  54.     give another.
  55.  
  56.         1) DUMB WAY: Put the object  file  in  a library and let the
  57.            library  routines  read the object file and  tell  what's
  58.            there:
  59.  
  60.            $ cxx tcxx
  61.            $ lib/crea tc tcxx
  62.            $ lib/list/ful/name tc
  63.            .
  64.            .
  65.            Module TCXX    Ident V1.0    Inserted 15-FEB-1996 19:21:46 1 symbol
  66.   -->      TCXX__XV
  67.  
  68.            (I've edited the library output for clarity)
  69.  
  70.  
  71.         2) A BIT  SMARTER:  Add  the  /MACHINE_CODE  "switch" to the
  72.            compile step, and look at the listing file produced:
  73.  
  74.            $ cxx tcxx/list/mach
  75.            $ type tcxx.lis
  76.  
  77. ----------------------------------------------------------------------------
  78.     1           #include <stdio.h>
  79.   675
  80.   676           void tcxx (void)
  81.                                           0010  TCXX__XV:
  82.        -->                          0000  0010          .entry  TCXX__XV,^m<>
  83.                               5E 04 C2    0012          subl2   #4,sp
  84. ----------------------------------------------------------------------------
  85.  
  86.            (Again, I've edited the  output.)  The  important line is
  87.            the  ".entry  TCXX__XV,^m<>" (and the one preceding  it).
  88.            The entry point name is what the linker sees.
  89.  
  90.         In order to _begin_ to do what you're attempting, you need to
  91.     write the Fortran as,
  92.  
  93.         Program Tfor
  94.         Call Tcxx__xv
  95.         End
  96.  
  97.     That will satisfy the LINK.  However, your printf may fail since the
  98.     C/C++ run time initialization hasn't  been  done given that the main
  99.     program is Fortran.  Post to comp.os.vms for pointers on getting the
  100.     initialization done, (it's simple, but I don't recall how to do it),
  101.     and be advised that you want to use _either_ Fortran I/O, _or_ C/C++
  102.     I/O,  but not _both_ in the same program...or at the very least, not
  103.     both to the same file (or terminal/keyboard).
  104.  
  105.         -Ken
  106. --
  107.  Kenneth H. Fairfield       |  Internet: Fairfield@Slac.Stanford.Edu
  108.  SLAC, P.O.Box 4349, MS 46  |  DECnet:   45537::FAIRFIELD (45537=SLACVX)
  109.  Stanford, CA   94309       |  Voice:    415-926-2924    FAX: 415-926-3515
  110.  -------------------------------------------------------------------------
  111.  These opinions are mine, not SLAC's, Stanford's, nor the DOE's...
  112.